const {
  Article
} = require('../../models/Article')

const { Comment } = require('../../models/Comment')
const path = require('path')
// 发布文章
const addArticle = async (ctx, next) => {
  let article = ctx.request.body
  await Article.create(article).then(res => {
    // console.log(res);
    ctx.body = {
      code: 200,
      msg: "发布成功"
    }
  }).catch(err => {
    ctx.body = {
      code: 300,
      msg: err
    }
  })

}

// 查询作者全部文章 带分页
const findAllArticle = async (ctx, next) => {
  let {
    page,
    author
  } = ctx.query
  // 页码进行判断
  if (!page || isNaN(Number(page))) {
    page = 1
  } else {
    page = Number(page)
  }
  // 每一页的条数
  let pageSize = 10
  // 计算总条数
  let res = await Article.find({
    author
  }).countDocuments()
  let totalPage = res ? Math.ceil(res / pageSize) : 0
  if (page >= totalPage) {
    page = totalPage
  } else if (page < 1) {
    page = 1
  }
  // 计算起始位置
  let start = (page - 1) * pageSize
  let data = await Article.find({
    author
  }).skip(start).limit(pageSize)
  if (data && data.length > 0) {
    ctx.body = {
      code: 200,
      total:res,
      page,
      pageSize,
      data
    }
  } else {
    ctx.body = {
      code: 300,
      msg: "查询失败"
    }
  }

}

// 查询单个文章 
const findOneArticle = async (ctx, next) => {

  let {
    id
  } = ctx.query
  try {
    let data = await Article.findOne({
      id
    })
    let commentList = await Comment.find({
      articleId: id
    })
    ctx.body = {
      code: 200,
      data,
      commentList
    }
    // 查询成功之后阅读量加一
    await Article.updateOne({
      id
    }, {
      $inc: {
        read: 1
      }
    })
  } catch (err) {
    ctx.body = {
      code: 300,
      msg: err.message
    }
  }

}

// 修改文章
const updateArticle = async (ctx, next) => {
  try {
    let {
      id,
      title,
      stemfrom,
      content
    } = ctx.request.body
    let data = await Article.updateOne({
      id
    }, {
      title,
      stemfrom,
      content
    })
    if (data.n > 0) {
      ctx.body = {
        code: 200,
        msg: "修改成功"
      }
    }

  } catch (err) {
    ctx.body = {
      code: 300,
      msg: "文章修改失败",
      err: err.message
    }
  }
}

// 删除单个文章 
const deleteArticle = async (ctx, next) => {
  let {
    id
  } = ctx.request.body
  try {
    let data = await Article.findOneAndDelete({
      id
    })
    if (!data) {
      ctx.body = {
        code: 300,
        msg: "id不存在"
      }
      return
    }
    ctx.body = {
      code: 200,
      msg: "删除成功"
    }
  } catch (err) {
    ctx.body = {
      code: 300,
      msg: err.message
    }
  }

}

// 富文本图片上传
const uploadArticle = async (ctx, next) => {
  const file = ctx.request.files
  let filePath = file['img'].path.split('\\').pop()
  let avatar = `${ctx.origin}/uploads/${filePath}`
  ctx.body = {
    errno: 0,
    data: [{
      url: avatar,
      alt: '',
      href: ''
    }]
  }
}


const find = async (ctx, next) => {
  let {
    page
  } = ctx.query
  // 页码进行判断
  if (!page || isNaN(Number(page))) {
    page = 1
  } else {
    page = Number(page)
  }
  // 每一页的条数
  let pageSize = 10
  // 计算总条数
  let res = await Article.find().countDocuments()
  let totalPage = res ? Math.ceil(res / pageSize) : 0
  if (page >= totalPage) {
    page = totalPage
  } else if (page < 1) {
    page = 1
  }
  // 计算起始位置
  let start = (page - 1) * pageSize
  let data = await Article.find().skip(start).limit(pageSize)
  if (data && data.length > 0) {
    ctx.body = {
      code: 200,
      total:res,
      page,
      pageSize,
      data
    }
  } else {
    ctx.body = {
      code: 300,
      msg: "查询失败"
    }
  }

}

module.exports = {
  addArticle,
  findAllArticle,
  findOneArticle,
  updateArticle,
  deleteArticle,
  uploadArticle,
  find
}